User-Defined Functions
User-defined functions incorporate all the features of user-defined procedures, but they have one additional feature which makes them extremely useful when writing scripts: an associated value. User-defined functions, unlike procedures, can pass data out of the subroutine through a return value, which associates the value with the subroutine identifier. This means that, like a variable, a function can be used wherever a value is required—in an expression, an assignment statement, or other operation in a script.
User-defined functions, like procedures, are declared between the definition blocks and the body of the script. To create a user-defined function, a function declaration statement will be used to associate an identifier with the subroutine and define how it will be used. The general syntax for user-defined functions is:
FUNCTION <procedure identifier>[(<parameter list>)]:<return value type>
Just like procedures, the declaration begins with a keyword, in this case the FUNCTION keyword, and is followed by the identifier to be associated with the subroutine block. Next comes the parameter list for the function. Parameter lists for user-defined functions work exactly like they do for user-defined procedures, so everything learned in the previous section applies here as well.
User-defined function declarations have one additional requirement: a return value type after the parameter list. This data type indicates what type of data will be passed through the return value mechanism and will be associated with the identifier.
After the function declaration has been created, define the actual working code of the subroutine in the same way you would for a user-defined procedure.
To illustrate the differences between procedure and function subroutines, look at the sum of squares example from the previous section:
 
The SumOfSquares subroutine provides a handy reusable piece of code which is very useful, but the result is returned to the main script in such a way that it is difficult for anyone reading the script code to determine how the value is obtained. In this instance, the return value mechanism of a function subroutine can be used to provide a much more user-friendly method. To create the function subroutine, the first step is to make some changes to the declaration statement:
 
The first change to the declaration is to convert the keyword from PROCEDURE to FUNCTION to indicate the correct type of subroutine. The output parameter result is then eliminated, since a return value will be used for the subroutine’s output. Next, a return value data type is added to the declaration.
Once the declaration statement has been modified, one additional change to the subroutine is needed to associate the result value with the subroutine identifier. VectorScript performs this association by using an assignment statement, except that the identifier used on the left side of the statement is the subroutine identifier:
 
All that is left to do now is to modify the main script to match the new syntax of the function:
 
As you can see, using a function subroutine in this instance makes for much more readable code, and simplifies the interface of the subroutine as well. In general, functions are best suited to subroutines which return a value that is the result of a calculation or other similar operation. Procedures should be used when creating a subroutine that performs an operation which does not return a value.

User Defined Functions : User-Defined Functions

Nemetschek NA
Phone: 410.290.5114
Fax: 410.290.8050